package us.gaaoc.framework.webapp.controllers;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.commons.lang.StringUtils;
import org.apache.myfaces.custom.datascroller.HtmlDataScroller;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.mapping.Join;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.Type;

import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordHistory;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.constants.Constants.Roles;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.controllers.model.TableView;
import us.gaaoc.framework.webapp.util.PagedListDataModel;

/**
 * This is the controller for the filings single table view.
 * 
 * The controller is configured with two main parameters.
 * 
 * 1) tableList
 *  This setting tells us what sub-tables of the hibernateView table we want the user to see. 
 *  This property is a List<TableView>.  This setting controls what different categories the user can see.
 *  
 *  The default tableView takes seven arguments
 *  	1) the ID
 *  	2) the visible description
 *  	3) statusCode1 - a status code to show
 *  	4) statusCode2 - a status code to show
 *  	5) statusCode3 - a status code to show
 *  	6) statusCode4 - a status code to show
 *  	7) statusCode5 - a status code to show
 *  
 *  The table list must have two TableViews with specific ID's:
 *  	rejectedByAttorney - Cases rejected by the next person or entity in the chain.
 * 		rejectedByClerk - Cases rejected by the next person or entity in the chain.
 *  	default - the default table to show. 
 * 
 * 
 * Faces configuration:
 * 		
 * <pre>
	<managed-bean>
		<managed-bean-name>nameOfBean</managed-bean-name>
		<managed-bean-class>
			us.gaaoc.framework.webapp.controllers.FilingsTableController
		</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
		<managed-property>			
  			<property-name>tableList</property-name>
  			<value>#{el-expression-of-bean}</value>  			
		</managed-property> 
	</managed-bean>
</pre>
 * 
 * @author artripa
 */
public class FilingsTableController extends BaseTableController {

	private static final long serialVersionUID = 1L;

	
	
	
	
	
	/* (non-Javadoc)
	 * @see us.gaaoc.framework.webapp.controllers.BaseTableController#getRole()
	 */
	@Override
	public int getRole() {
		return 6; //filer
	}

	/* Getters */

	public int getRejectedCount() throws HibernateException, ClassNotFoundException {
		return this.getDataSize(this.findtable("rejectedByAttorney")) + 
				this.getDataSize(this.findtable("rejectedByClerk"));
	}
	
	/* Navigation */
	
	/**
	 * Navigates the user to the case details page. 
	 * @requestparam caseUuid the UUID of the case.
	 */
	@SuppressWarnings("unchecked")
	public String gotoCaseDetailsPage() {
		Map<String, String> sessionMap = this.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "allFilingsTablesPage");
	 	
	 	return "gotoCSECaseDetailsPage";
	}
	
	
	public void setCaseUuid(String caseUuid) {
		Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();	 	
	 	sessionMap.put("caseUuid", caseUuid);
	 	sessionMap.put("from", "trashcanPage");	 	
	}
	public void clearCase(String caseUuid){
		this.getModelFacade().updateCaseRecordForClearCase(caseUuid, true, this.getCourtUuid());
	}
	public String clearCase()
	{
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		this.getModelFacade().updateCaseRecordForClearCase(caseUuid, true, this.getCourtUuid());
			
		return "stay";
	}
	
	/**
	 * Navigates the user to the trash can page.
	 */
	public String gotoTrashCanPage() {
		return "trashcanPage";
	}
	
	/**
	 * Navigates the user to the existing filing page.
	 */
	public String gotoExistingFilingPage() {
		return "existingFilingPage";
	}
	
	/**
	 * Navigates the user to the search page.  Also, saves the current page
	 * to the session map.
	 */
	@SuppressWarnings("unchecked")
	public String gotoSearchPage() {
		Map<String, String> sessionMap = this.getSessionMap();
	 	sessionMap.put("from", "agentFilingsTablesPage");
		return "gotoSearchPage";
	}
	
	
	/**
	 * Navigates the user to the subsequent Filing page.  Also, saves the current page
	 * to the session map.
	 */
	@SuppressWarnings("unchecked")
	public String gotoSubsequentFilingPage() {
		Map<String, String> sessionMap = this.getSessionMap();
	 	sessionMap.put("from", "agentFilingsTablesPage");
	 	sessionMap.put("displayUserMessageVarible", "true");

		return "gotoSubsequentFilingPage";
	}
	
	/**
	 * In the future, this page will navigate the user to the reports page. Currently,
	 * the method simply returns the user to the current page.
	 */
	@SuppressWarnings("unchecked")
	public String gotoReportsPage() {
		Map<String, String> sessionMap = this.getSessionMap();
	 	sessionMap.put("from", "agentFilingsTablesPage");
		return "gotoReportsPage";
	}
	
	/**
	 * Navigates the user to the new filings page.  Also, saves the current page
	 * to the session map.
	 */
	@SuppressWarnings("unchecked")
	public String createNewFiling() {
		Map<String, String> sessionMap = this.getSessionMap();
		sessionMap.put("from", "allFilingsPage");
		sessionMap.remove("caseUuid"); // fix for issue GAJETWOX-124
		return "createNewFilingPage";
	}

	/**
	 * Navigates to the history of the case
	 * @requestparam caseUuid the UUID of the case record
	 */
	@SuppressWarnings("unchecked")
	public String gotoHistoryPage() {
		Map<String, String> sessionMap = this.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "AllFilingsTablePage");
		return "gotoCaseHistoryPage";
	}
	



}
